package com.huawei.secure.android.common.ssl;

import android.content.Context;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class SecureX509TrustManager implements X509TrustManager {
    private static final String P = "AndroidCAStore";
    private static final String TAG = "SecureX509TrustManager";
    private static final String a = "X509";
    private static final String b = "bks";
    private static final String k = "hmsrootcas.bks";
    private static final String l = "";
    private X509Certificate[] Q;
    protected List<X509TrustManager> c;

    public SecureX509TrustManager(Context context) throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IllegalArgumentException {
        this(context, false);
    }

    public SecureX509TrustManager(Context context, boolean z) throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IllegalArgumentException {
        this.c = new ArrayList();
        if (context == null) {
            throw new IllegalArgumentException("context is null");
        }
        if (z) {
            a();
        }
        e(context);
        if (this.c.isEmpty()) {
            throw new CertificateException("X509TrustManager is empty");
        }
    }

    public SecureX509TrustManager(InputStream inputStream, String str) throws IllegalArgumentException {
        this.c = new ArrayList();
        a(inputStream, str);
    }

    public SecureX509TrustManager(String str) throws IllegalArgumentException, FileNotFoundException {
        this(str, false);
    }

    public SecureX509TrustManager(String str, boolean z) throws IllegalArgumentException, FileNotFoundException {
        InputStream fileInputStream;
        Throwable th;
        this.c = new ArrayList();
        try {
            fileInputStream = new FileInputStream(str);
            try {
                a(fileInputStream, "");
                d.a(fileInputStream);
                if (z) {
                    a();
                }
            } catch (Throwable th2) {
                th = th2;
                d.a(fileInputStream);
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            fileInputStream = null;
            d.a(fileInputStream);
            throw th;
        }
    }

    public SecureX509TrustManager(InputStream inputStream, String str, boolean z) throws IllegalArgumentException {
        this.c = new ArrayList();
        a(inputStream, str);
        if (z) {
            a();
        }
    }

    private void a() {
        Exception e;
        try {
            KeyStore instance = KeyStore.getInstance(P);
            instance.load(null, null);
            TrustManagerFactory instance2 = TrustManagerFactory.getInstance(a);
            instance2.init(instance);
            TrustManager[] trustManagers = instance2.getTrustManagers();
            for (int i = 0; i < trustManagers.length; i++) {
                if (trustManagers[i] instanceof X509TrustManager) {
                    this.c.add((X509TrustManager) trustManagers[i]);
                }
            }
        } catch (KeyStoreException e2) {
            e = e2;
            Log.e(TAG, "loadSystemCA: exception : " + e.getMessage());
        } catch (CertificateException e3) {
            e = e3;
            Log.e(TAG, "loadSystemCA: exception : " + e.getMessage());
        } catch (IOException e4) {
            e = e4;
            Log.e(TAG, "loadSystemCA: exception : " + e.getMessage());
        } catch (NoSuchAlgorithmException e5) {
            e = e5;
            Log.e(TAG, "loadSystemCA: exception : " + e.getMessage());
        }
    }

    private void e(Context context) {
        Exception e;
        try {
            TrustManagerFactory instance = TrustManagerFactory.getInstance(a);
            KeyStore instance2 = KeyStore.getInstance(b);
            InputStream open = context.getAssets().open("hmsrootcas.bks");
            open.reset();
            instance2.load(open, "".toCharArray());
            instance.init(instance2);
            TrustManager[] trustManagers = instance.getTrustManagers();
            for (int i = 0; i < trustManagers.length; i++) {
                if (trustManagers[i] instanceof X509TrustManager) {
                    this.c.add((X509TrustManager) trustManagers[i]);
                }
            }
            d.a(open);
        } catch (KeyStoreException e2) {
            e = e2;
            try {
                Log.e(TAG, "loadBksCA: exception : " + e.getMessage());
            } finally {
                d.a(null);
            }
        } catch (CertificateException e3) {
            e = e3;
            Log.e(TAG, "loadBksCA: exception : " + e.getMessage());
        } catch (IOException e4) {
            e = e4;
            Log.e(TAG, "loadBksCA: exception : " + e.getMessage());
        } catch (NoSuchAlgorithmException e5) {
            e = e5;
            Log.e(TAG, "loadBksCA: exception : " + e.getMessage());
        }
    }

    private void a(InputStream inputStream, String str) {
        Exception e;
        if (inputStream == null || str == null) {
            throw new IllegalArgumentException("inputstream or trustPwd is null");
        }
        try {
            TrustManagerFactory instance = TrustManagerFactory.getInstance(a);
            KeyStore instance2 = KeyStore.getInstance(b);
            instance2.load(inputStream, str.toCharArray());
            instance.init(instance2);
            TrustManager[] trustManagers = instance.getTrustManagers();
            for (int i = 0; i < trustManagers.length; i++) {
                if (trustManagers[i] instanceof X509TrustManager) {
                    this.c.add((X509TrustManager) trustManagers[i]);
                }
            }
            d.a(inputStream);
        } catch (KeyStoreException e2) {
            e = e2;
            try {
                Log.e(TAG, "loadInputStream: exception : " + e.getMessage());
            } finally {
                d.a(inputStream);
            }
        } catch (CertificateException e3) {
            e = e3;
            Log.e(TAG, "loadInputStream: exception : " + e.getMessage());
        } catch (IOException e4) {
            e = e4;
            Log.e(TAG, "loadInputStream: exception : " + e.getMessage());
        } catch (NoSuchAlgorithmException e5) {
            e = e5;
            Log.e(TAG, "loadInputStream: exception : " + e.getMessage());
        }
    }

    public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
        Log.i(TAG, "checkClientTrusted: ");
        for (X509TrustManager checkServerTrusted : this.c) {
            try {
                checkServerTrusted.checkServerTrusted(x509CertificateArr, str);
                return;
            } catch (CertificateException e) {
                Log.e(TAG, "checkServerTrusted CertificateException" + e.getMessage());
            }
        }
        throw new CertificateException("checkServerTrusted CertificateException");
    }

    public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
        setChain(x509CertificateArr);
        Log.i(TAG, "checkServerTrusted: begin");
        int size = this.c.size();
        int i = 0;
        while (i < size) {
            try {
                Log.i(TAG, "checkServerTrusted: check server i : " + i);
                ((X509TrustManager) this.c.get(i)).checkServerTrusted(x509CertificateArr, str);
                return;
            } catch (CertificateException e) {
                Log.e(TAG, "checkServerTrusted error :" + e.getMessage() + " , time : " + i);
                if (i == size - 1) {
                    throw e;
                }
                i++;
            }
        }
    }

    public X509Certificate[] getAcceptedIssuers() {
        try {
            ArrayList arrayList = new ArrayList();
            for (X509TrustManager acceptedIssuers : this.c) {
                arrayList.addAll(Arrays.asList(acceptedIssuers.getAcceptedIssuers()));
            }
            return (X509Certificate[]) arrayList.toArray(new X509Certificate[arrayList.size()]);
        } catch (Exception e) {
            Log.e(TAG, "getAcceptedIssuers exception : " + e.getMessage());
            return new X509Certificate[0];
        }
    }

    public X509Certificate[] getChain() {
        return this.Q;
    }

    public void setChain(X509Certificate[] x509CertificateArr) {
        this.Q = x509CertificateArr;
    }
}
